<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>账号列表</span>
    </div>

    <!-- 内容部分 -->
    <div>
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="account" label="账号"></el-table-column>
        <el-table-column prop="userGroup" label="用户组"> </el-table-column>
        <el-table-column prop="ctime" label="创建时间"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="primary" size="mini" @click="handleData(row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="del(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </div>

    <!-- 按钮 -->
    <div>
      <el-button size="mini" type="danger" @click="delUsers()"
        >批量删除</el-button
      >
      <el-button size="mini" @click="toggleSelection()" type="primary"
        >取消选择</el-button
      >
    </div>

    <!-- 对话框 -->
    <div>
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-form
          :model="editForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="70px"
          class="demo-ruleForm"
          size="mini"
        >
          <el-form-item label="账号" prop="uname">
            <el-input v-model="editForm.uname"></el-input>
          </el-form-item>
          <el-form-item label="用户组" prop="userGroup">
            <el-select v-model="editForm.userGroup" placeholder="请选择">
              <el-option label="普通管理员" value="普通管理员"> </el-option>
              <el-option label="超级管理员" value="超级管理员"> </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editDone('ruleForm')"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import { getData, delData, delDatas, editDatas } from '../../api/user.js'
import { checkUName } from '@/utils/validate.js'
import moment from 'moment'
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 3,
      total: 0,
      tableData: [],
      multipleSelection: [],
      dialogVisible: false,
      editForm: {
        uname: '',
        userGroup: ''
      },
      rules: {
        uname: [{ validator: checkUName, trigger: 'blur' }],
        userGroup: []
      }
    }
  },

  created() {
    this.getDataList()
  },

  methods: {
    // 编辑功能
    handleData(obj) {
      console.log(obj)
      this.editForm.uname = obj.account
      this.editForm.userGroup = obj.userGroup
      this.editForm.id = obj.id
      this.dialogVisible = true
    },
    editDone(str) {
      this.$refs[str].validate(async valid => {
        if (valid) {
          // 表单验证通过
          const data = await editDatas({
            id: this.editForm.id,
            account: this.editForm.uname,
            userGroup: this.editForm.userGroup
          })
          if (data.code === 0) {
            this.$message({
              type: 'success',
              message: data.msg
            })
            this.$refs[str].resetFields() // 清空输入框
            this.dialogVisible = false
            this.getDataList()
          }
        } else {
          this.$message.error('账号名不能为空')
        }
      })
    },
    // 对话框
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    async getDataList() {
      const { data, total } = await getData({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      // console.log(data, total)
      this.tableData = data
      this.total = total
    },
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const data = await delData({ id })
          if (data.code === 200) {
            this.$message({
              type: 'success',
              message: data.msg
            })
            this.getDataList()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async delUsers() {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const arr = this.multipleSelection.map(i => {
            return i.id
          })
          const ids = JSON.stringify(arr)
          const { code, msg } = await delDatas({
            ids
          })
          if (code === 0) {
            this.$message({
              type: 'success',
              message: msg
            })
          } else {
            this.$message.error(msg)
          }
          this.getDataList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.pageSize = val
      // console.log(`每页 ${val} 条`)
      this.getDataList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      // console.log(`当前页: ${val}`)
      this.getDataList()
    }
  },
  watch: {
    dialogVisible() {
      if (!this.dialogVisible) {
        this.$refs.ruleForm.resetFields()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-select {
  width: 413px;
}
</style>
